package net.kosev.battleships.model.ai;

import java.util.ArrayList;
import java.util.Random;

import net.kosev.battleships.model.Board;
import net.kosev.battleships.model.Field;

/**
 * Easy Computer AI
 * 
 * @author Nikola
 *
 */
public class EasyEnemyAI implements EnemyAI {
	
	/**
	 * Board instance
	 */
	protected Board board = null;

	/* (non-Javadoc)
	 * @see net.kosev.battleships.model.ai.EnemyAI#setBoard(net.kosev.battleships.model.Board)
	 */
	public void setBoard(Board board) {
		this.board = board;
	}
	
	/*
	 * (non-Javadoc)
	 * @see net.kosev.battleships.model.ai.EnemyAI#getFirePosition()
	 */
	public int getFirePosition() {
		ArrayList<Integer> freePositions = new ArrayList<Integer>();
		int[][] fields = board.getFields();
		
		for (int i = 0; i < Board.SIZE; i++) {
			for (int j = 0; j < Board.SIZE; j++) {
				if (fields[i][j] == Field.WATER || fields[i][j] == Field.SHIP) {
					freePositions.add(i * Board.SIZE + j);
				}
			}
		}
		
		return freePositions.get(new Random().nextInt(freePositions.size()));
	}

}
